<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>EASY后台-菜单</title>
    <link rel="icon" th:href="@{/static/images/favicon.ico}">
    <link rel="stylesheet" th:href="@{/static/css/common.css}">
    <link rel="stylesheet" th:href="@{/static/layui/css/layui.css}">
</head>
<body>
<!-- 页面内容 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 搜索条件 -->
            <form class="layui-form">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">标题</label>
                        <div class="layui-input-inline">
                            <input type="text" name="title" placeholder="请输入" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">类型</label>
                        <div class="layui-input-inline">
                            <select name="type" th:with="dataList=${@dictService.listData('MENU_TYPE')}">
                                <option value="">请选择</option>
                                <option th:each="dict : ${dataList}" th:text="${dict.dictItem}" th:value="${dict.dictValue}"></option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn layui-btn-md" lay-submit lay-filter="tableSearch">
                            <i class="layui-icon layui-icon-search"></i>
                            查询
                        </button>
                        <button type="reset" class="layui-btn layui-btn-primary layui-btn-md">
                            <i class="layui-icon layui-icon-refresh"></i>
                            重置
                        </button>
                    </div>
                </div>
            </form>
            <!-- 表格 -->
            <table class="layui-hide" id="dataTable" lay-filter="dataTable"></table>
        </div>
    </div>
</div>
</body>

<!-- 表格工具 -->
<script type="text/html" id="tableToolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">
            <i class="layui-icon layui-icon-addition"></i>新建
        </button>
        <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="batchRemove">
            <i class="layui-icon layui-icon-delete"></i>删除
        </button>
    </div>
</script>

<!-- 表格行工具 -->
<script type="text/html" id="tableRowTool">
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="remove">删除</a>
    {{# if (d.enableFlag == true) { }}
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="enable">禁用</a>
    {{# } else { }}
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="enable">启用</a>
    {{# } }}
</script>

<script th:src="@{/static/js/jquery.js}"></script>
<script th:src="@{/static/layui/layui.js}"></script>
<script>
    layui.use(['element', 'layer', 'treeTable', 'laydate', 'form'], function () {
        var element = layui.element;
        var layer = layui.layer;
        var treeTable = layui.treeTable;
        var laydate = layui.laydate;
        var form = layui.form;


        // table渲染
        var tableObj = treeTable.render({
            elem: '#dataTable',
            url: '/sysMenu/list',
            method: 'GET',
            where: { // 添加额外条件

            },
            tree: {
                customName: {
                    name: 'title',
                    icon: 'ic'
                }
            },
            autoSort: false,
            toolbar: '#tableToolbar',
            defaultToolbar: ['filter', 'exports', 'print'],
            cols: [
                [
                    {type: 'checkbox', fixed: 'left'},
                    {type: 'numbers', title: '序号', align: 'center'},
                    {field: 'title', title: '标题', width: 200, align: 'center'},
                    {
                        field: 'type', title: '类型', width: 120, align: 'center',
                        templet: function (row) {
                            return row.typeDesc;
                        }
                    },
                    {field: 'parentName', title: '父级菜单', width: 150, align: 'center'},
                    {field: 'icon', title: '图标', width: 120, align: 'center'},
                    {field: 'route', title: '路由', width: 180, align: 'center'},
                    {field: 'code', title: '编码', width: 120, align: 'center'},
                    {field: 'orderNum', title: '排序号', width: 100, align: 'center'},
                    {
                        field: 'enableFlag', title: '是否启用', width: 120, align: 'center',
                        templet: function (row) {
                            return row.enableFlag === true ? '是' : '否';
                        }
                    },
                    {field: 'remark', title: '备注', width: 150, align: 'center'},
                    {field: 'createTime', title: '创建时间', width: 200, align: 'center'},
                    {fixed: 'right', title: '操作', minWidth: 240, align: 'center', templet: '#tableRowTool'}
                ]
            ],
            page: false,
            loading: true,
            skin: 'grid', // 边框风格
            size: 'md', // 尺寸
            even: true, // 隔行背景
            before: function (options) { // 数据渲染之前的回调函数

            },
            done: function (res, curr, count, origin) { // 数据渲染完毕的回调函数

            },
            error: function (e, msg) { // 数据请求失败的回调函数

            },
            parseData: function (res) { // 数据格式解析的回调函数
                return {
                    "code": res.code === 200 ? 0 : res.code,
                    "msg": res.msg,
                    "data": res.data
                }
            },
        });

        // 触发排序事件
        treeTable.on('sort(dataTable)', function (obj) {
            treeTable.reload('dataTable', {
                initSort: obj,
                where: {
                    prop: obj.field,
                    order: obj.type
                }
            });
        });

        // 监听表格工具条
        treeTable.on('toolbar(dataTable)', function (obj) {
            if (obj.event === 'add') {
                layer.open({
                    type: 2,
                    area: ['800px', '400px'],
                    title: '新建菜单',
                    shadeClose: true,
                    scrollbar: false,
                    content: '/sysMenu/addForm'
                });
            }
            if (obj.event === 'batchRemove') {
                var checkRows = treeTable.checkStatus('dataTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请先选择数据', {icon: 0});
                    return;
                }
                layer.confirm('确定删除所选数据吗？', {
                    btn: ['确定', '关闭']
                }, function () {
                    var ids = checkRows.data.map(function (row) {
                        return row.id;
                    });
                    $.ajax({
                        url: '/sysMenu/remove',
                        type: 'DELETE',
                        contentType: 'application/json',
                        dataType: 'json',
                        data: JSON.stringify({idList: ids}),
                        success: function (response) {
                            if (response.code === 200) {
                                layer.msg('删除成功', {icon: 1});
                                tableObj.reload();
                            } else {
                                layer.msg(response.msg, {icon: 0});
                            }
                        },
                        error: function (xhr, status, error) {
                            layer.msg('请求失败', {icon: 0});
                        }
                    });
                });
            }
        });

        // 监听表格行工具
        treeTable.on('tool(dataTable)', function (obj) {
            if (obj.event === 'edit') {
                layer.open({
                    type: 2,
                    area: ['800px', '400px'],
                    title: '编辑菜单',
                    shadeClose: true,
                    scrollbar: false,
                    content: '/sysMenu/editForm/' + obj.data.id
                });
            }
            if (obj.event === 'remove') {
                layer.confirm('确定删除所选数据吗？', {
                    btn: ['确定', '关闭']
                }, function () {
                    $.ajax({
                        url: '/sysMenu/remove',
                        type: 'DELETE',
                        contentType: 'application/json',
                        dataType: 'json',
                        data: JSON.stringify({idList: [obj.data.id]}),
                        success: function (response) {
                            if (response.code === 200) {
                                layer.msg('删除成功', {icon: 1});
                                tableObj.reload();
                            } else {
                                layer.msg(response.msg, {icon: 0});
                            }
                        },
                        error: function (xhr, status, error) {
                            layer.msg('请求失败', {icon: 0});
                        }
                    });
                });
            }
            if (obj.event === 'enable') {
                $.ajax({
                    url: '/sysMenu/enable/' + obj.data.id,
                    type: 'PUT',
                    contentType: 'application/json',
                    dataType: 'json',
                    success: function (response) {
                        if (response.code === 200) {
                            layer.msg('请求成功', {icon: 1});
                            tableObj.reload();
                        } else {
                            layer.msg(response.msg, {icon: 0});
                        }
                    },
                    error: function (xhr, status, error) {
                        layer.msg('请求失败', {icon: 0});
                    }
                });
            }
        });

        // 监听查询
        form.on('submit(tableSearch)', function (data) {
            tableObj.reload({where: data.field});
            return false;
        });
    });
</script>
</html>